package com.thinklib.util;

import com.sun.mail.util.MailSSLSocketFactory;
import org.springframework.beans.factory.annotation.Value;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * 邮件发送工具类
 */
public class MailUtil implements Runnable {

	@Value("mail.username")
	private String mailUsername;

	@Value("mail.password")
	private String mailPassword;

	@Value("mail.host")
	private String host;

	@Value("mail.subject")
	private String mailSubject;

	/*=========收件人的信息==========*/
	private String email;// 收件人邮箱
	private String code;// 激活码
	/*=========初始化===============*/
	public MailUtil(String email, String code) {
		this.email = email;
		this.code = code;
	}

	public void run() {
		// 指定发送邮件的主机smtp.qq.com(QQ)、smtp.163.com(网易)
		Properties properties = System.getProperties();// 获取系统属性
		properties.setProperty("mail.smtp.host", host);// 设置邮件服务器
		properties.setProperty("mail.smtp.auth", "true");// 打开认证

		try {
			//QQ邮箱需要下面这段代码，163邮箱不需要
			MailSSLSocketFactory sf = new MailSSLSocketFactory();
			sf.setTrustAllHosts(true);
			properties.put("mail.smtp.ssl.enable", "true");
			properties.put("mail.smtp.ssl.socketFactory", sf);
			
			
			// 1.获取默认session对象（创建连接对象，连接到邮箱服务器）
			Session session = Session.getInstance(properties, new Authenticator() {
				public PasswordAuthentication getPasswordAuthentication() {
					// 发件人邮箱账号、密码
					return new PasswordAuthentication(mailUsername, mailPassword);
				}
			});

			// 2.创建邮件对象
			Message message = new MimeMessage(session);
			// 2.1设置发件人
			message.setFrom(new InternetAddress(mailUsername));
			// 2.2设置接收人
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
			// 2.3设置邮件主题
			message.setSubject(mailSubject);
			// 2.4设置邮件内容
			String content = "<!DOCTYPE html><html><head><meta charset=\"UTF-8\"><title>光影图书馆账号激活邮件</title><style type=\"text/css\">html, body {margin: 0;padding: 0;}.box {width: 600px;height: 500px;border: 1px solid rgb(204, 204, 204);border-top-color: red;margin: 0 auto;}.title {height: 80px;background-color: rgb(251, 251, 251);line-height: 80px;text-indent: 40px;font-weight: bold;}.cl {color: rgb(0, 120, 215);}.act-desc {margin-top: 20px;font-size: 15px;font-weight: bold;text-indent: 40px;}.act {height: 150px;text-align: center;font-size: 18px;}.tips {height: 200px;text-indent: 40px;margin-top: 20px;font-size: 12px;}.btn {margin-top: 50px;width: 110px;height: 44px;line-height: 44px;border: none;padding: 0;box-shadow: none;border-radius: 5px;letter-spacing: 1px;background-color: rgb(20,120,245);color: white;font-size: 17px;cursor:pointer;outline:0 none !important;}</style></head><body><div class=\"box\"><div class=\"title\"><h2><span class=\"cl\">光影图书馆</span>&nbsp;-&nbsp;账号激活服务</h2></div><div class=\"act-desc\"><div>尊敬的" + email + "（登录账号名），您好！</div><div>感谢您注册光影图书馆。为激活您的账号，请单击下列链接进行账号激活。</div></div><div class=\"act\"><a href=\"http://localhost:8080/api/v1/user/activation?code="+code+"\"><button class=\"btn\">立即激活</button></a></div><div class=\"tips\"><div>温馨提示：</div><div>1、如果单击激活按钮没有反应，请在激活按钮上单击右键，选择复制链接地址到浏览器窗口打开。</div><div>2、为了账户安全，请妥善保存您的密码，并定期更新密码。</div><div>3、光影图书馆官方网址：thinklib.com</div><div>4、光影图书馆登录方式：在logo上双击即可弹出登录框</div><div>5、光影图书馆注册方式：鼠标指向logo，1秒后，弹出\"感受读书好时光\"提示框，点击提示框即可</div></div></div></body></html>";
			message.setContent(content, "text/html;charset=UTF-8");
			
			// 3.发送邮件
			Transport.send(message);
			System.out.println("邮件成功发送!");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
